//
//  TopViewController.m
//  Movie
//
//  Created by tengyang on 15/7/17.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "TopViewController.h"
#import "DataService.h"
#import "TopModal.h"
#import "TopCollectionViewCell.h"
//#import "DiscussTableView.h"
#import "DiscussViewController.h"

#define kWidth self.view.bounds.size.width
#define kHeight self.view.bounds.size.height
@interface TopViewController ()

@end

@implementation TopViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title=@"Top";
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createCollectionView];
    [self _loadJson];
}


- (void)_loadJson
{
    NSDictionary *temp=[DataService getJsonDataFromFile:@"top250.json"];
    NSArray *subjects=[temp objectForKey:@"subjects"];
    _topModalArray =[[NSMutableArray alloc]init];
    for (NSDictionary *dic in subjects) {
        TopModal *modal=[[TopModal alloc]init];
        modal.title=[dic objectForKey:@"title"];
        NSDictionary *temp=[dic objectForKey:@"rating"];
        NSString *str=[temp objectForKey:@"average"];
        modal.average=str.floatValue;
        modal.images=[dic objectForKey:@"images"];
        [_topModalArray addObject:modal];
    }
}


- (void)_createCollectionView
{
    
    //使用UICollectionView来显示多个模块的图例，配合UICollectionViewFlowLayout来改变图例的左右间距和上下间距
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    //设置图例的大小
    layout.itemSize=CGSizeMake(100, 170);
    layout.minimumInteritemSpacing=20;
    layout.minimumLineSpacing=30;
    
    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(10, 20, kWidth-20, kHeight-20) collectionViewLayout:layout];
    //设置背景图片
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    imageView.image=[UIImage imageNamed:@"bg_main"];
    [_collectionView setBackgroundView:imageView];
    
    
    _collectionView.dataSource=self;
    _collectionView.delegate=self;
    [self.view addSubview:_collectionView];
    
    //加载xib
    UINib *nib=[UINib nibWithNibName:@"TopCollectionViewCell" bundle:[NSBundle mainBundle]];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell1"];
                
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _topModalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    TopModal *modal=_topModalArray[indexPath.row];
    cell.modal=modal;
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DiscussViewController *vc1=[[DiscussViewController alloc]init];
    [self.navigationController pushViewController:vc1 animated:YES];
    
    
    //[self.navigationController pushViewController:tableView animated:YES];
}

//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(10, 10, 10, 10);
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
